package com.petsbang.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout.LayoutParams;
import android.widget.HeaderViewListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.FrameLayout.LayoutParams;

import com.petsbang.action.ImageZoomActivity;
import com.petsbang.action.LovelyActivity;
import com.petsbang.action.R;
import com.petsbang.action.ShareActivity;
import com.petsbang.action.dao.DataApplication;
import com.petsbang.common.Common;
import com.petsbang.http.DataThread;
import com.petsbang.ui.FriendlyScrollView;
import com.petsbang.utils.AsyncImageLoader;
import com.petsbang.utils.AsyncImageLoader.ImageCallback;
import com.petsbang.vo.PictureInfo;
import com.petsbang.vo.UserShareInfo;

public class lovelyAdapter extends BaseAdapter {
	int mGalleryItemBackground;
	private LovelyActivity mContext;
	LayoutInflater mInflater;
	ArrayList<PictureInfo> picList;
	// public ImageLoader imageLoader;
	AsyncImageLoader imageLoader;
	ViewHolder holder;
	int index;

	HashMap<String, List<UserShareInfo>> shareHash;
	private MotionEvent e;
	boolean isDown;

	public lovelyAdapter(Context context,String fromFlag) {
		mContext = (LovelyActivity) context;
		imageLoader = new AsyncImageLoader();
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		DataApplication appState = (DataApplication) context.getApplicationContext();
		if (fromFlag == null || fromFlag.equals("")) {
			picList = appState.getPicList();
		} else {
			picList = appState.getPicFavorList();
		}
		
	}

	public void setIndex(int index01, int index02, int index03) {
		index = index02;
		imageLoader.setShowIndex(picList.get(index01).getLarge_url(), picList.get(index02).getLarge_url(), picList.get(index03).getLarge_url(), picList);

		//nodifyShareData(picList.get(index01).getLarge_url());
		//nodifyShareData(picList.get(index02).getLarge_url());
		//nodifyShareData(picList.get(index03).getLarge_url());

		notifyDataSetChanged();
	}

	public int getCount() {
		return picList.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}
	
	public void nodifyPicData(String imageUrl){
		
	}


	public void nodifyShareData(String imageUrl) {
		ListView iv = (ListView) mContext.gallery.findViewWithTag(imageUrl + "list");
		ProgressBar shareBar = (ProgressBar) mContext.gallery.findViewWithTag(imageUrl + "shareBar");
		
		DataApplication appState = (DataApplication) mContext.getApplicationContext();
		ArrayList<UserShareInfo> dataList;
		if (appState.getShareList().containsKey(imageUrl)) {
			if (shareBar != null) {
				shareBar.setVisibility(View.INVISIBLE);
			}
			
			dataList = appState.getShareList().get(imageUrl);

			if (dataList != null && dataList.size() > 0) {
				if (iv == null) {
					return;
				}
				
				int size = dataList.size();
				ArrayList<UserShareInfo> tmpList = new ArrayList<UserShareInfo>();
				if (size > 5) {
					for (int i = 0; i < 5; i++) {
						tmpList.add(dataList.get(i));
					}
				} else {
					tmpList = dataList;
				}
				iv.setFocusable(false);
				HeaderViewListAdapter listAdapter = (HeaderViewListAdapter) iv.getAdapter();
				UserShareAdapter usa = (UserShareAdapter) listAdapter.getWrappedAdapter();
				usa.nodifyShareData(tmpList);
				iv.setFocusable(false);
				LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, tmpList.size() * 140);
				iv.setLayoutParams(lp);
				iv.setVisibility(View.VISIBLE);
			}else{
				//获取失败显示失败信息
				TextView badView = (TextView)mContext.gallery.findViewWithTag(imageUrl + "badView");
				if (badView != null) {
					badView.setVisibility(View.VISIBLE);
					badView.setText(R.string.share_error);
				}
			}
		}else{
			//不存在时候网络获取
			String shareUrl = "http://api.snssdk.com/data/get_comments/?data_url=" + imageUrl + "&data_title=''&item_type=1&offset=0&count=10";
			DataThread thread = new DataThread(mContext, shareUrl, Common.share_type,null);
			thread.start();
		}
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		DataApplication appState = (DataApplication) mContext.getApplicationContext();
		PictureInfo tmpPic = picList.get(position);
		final String imageUrl = tmpPic.getLarge_url();
		ArrayList<UserShareInfo> dataList;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.lovely_item, null);
			holder = new ViewHolder();
			holder.list = (ListView) convertView.findViewById(R.id.shareList);

			// 设置列表底部视图
			View loadMoreView = mInflater.inflate(R.layout.load_more, null);
			Button loadMoreButton = (Button) loadMoreView.findViewById(R.id.loadMoreButton);
			loadMoreButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent mIntent = new Intent(mContext, ShareActivity.class);
					mIntent.putExtra("imageUrl", imageUrl);
					mContext.startActivity(mIntent);
				}
			});
			holder.list.addFooterView(loadMoreView, null, true);
			if (!appState.getShareList().containsKey(imageUrl)) {
				dataList = new ArrayList<UserShareInfo>();
			}else{
				dataList = appState.getShareList().get(imageUrl);
			}

			UserShareAdapter adapter = new UserShareAdapter(mContext, dataList, holder.list);
			holder.list.setAdapter(adapter);
			holder.list.setVisibility(View.INVISIBLE);

			holder.shareBar =  (ProgressBar) convertView.findViewById(R.id.shareBar);
			holder.shareBar.setVisibility(View.VISIBLE);
			
			holder.badView =  (TextView) convertView.findViewById(R.id.badView);
			holder.badView.setVisibility(View.INVISIBLE);
			
			holder.text = (TextView) convertView.findViewById(R.id.text);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.icon.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						isDown = true;
						e = event;
						return true;
					}

					if (event.getAction() == MotionEvent.ACTION_MOVE) {
						if (Math.abs(event.getX() - e.getX()) > 20 || Math.abs(event.getY() - e.getY()) > 20) {
							isDown = false;
						} else {
							return true;
						}
					}

					if (event.getAction() == MotionEvent.ACTION_UP) {
						if (isDown) {
							Intent intent = new Intent(mContext, ImageZoomActivity.class);
							Bundle bundle = new Bundle();
							bundle.putString("imageUrl", imageUrl);
							intent.putExtras(bundle);
							mContext.startActivity(intent);
							return true;
						}
					}

					return false;
				}
			});
			
//			LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(    
//		            LinearLayout.LayoutParams.FILL_PARENT,    
//		           140);
//			
//			convertView.setLayoutParams(mLayoutParams);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.icon.setBackgroundResource(R.drawable.default_image);
		holder.badView.setTag(imageUrl + "badView");
		holder.shareBar.setTag(imageUrl + "shareBar");
		holder.list.setTag(imageUrl + "list");
		holder.icon.setTag(imageUrl);
		holder.text.setText("" + tmpPic.getDesc());
		
		if (index != position) {
			return convertView;
		}
		
		nodifyShareData(imageUrl);
		
		Bitmap bm = imageLoader.loadDrawable(mContext, imageUrl, new ImageCallback() {

			@Override
			public void imageLoaded(Bitmap imageDrawable, String imageUrl) {
				ImageView iv = (ImageView) mContext.gallery.findViewWithTag(imageUrl);
				if (iv != null ) {
					if (imageDrawable != null) {
						iv.setImageBitmap(imageDrawable);
					} else{
						iv.setImageResource(R.drawable.ic_launcher);
					}
				}
			}
		});

		if (bm != null) {
			BitmapDrawable bd = new BitmapDrawable(mContext.getResources(), bm);
			holder.icon.setBackgroundDrawable(bd);
		}

		return convertView;
	}

	class ViewHolder {
		TextView text;
		ImageView icon;
		ListView list;
		TextView badView;
		ProgressBar shareBar;
	}

}
